﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZP.Common;
using ZP.DTO.Common;

namespace ZPJWeb.Controllers
{
    public class PublicUploadController : Controller
    {
        /// <summary>
        /// 营业执照-上传解析内容
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadAnalysisYYZZ()
        {
            ResultData<BusinessResult> result = new ResultData<BusinessResult>();

            var oFile = Request.Files[0];
            //var type = Request.Form["type"];
            string errorMsg = "";
            if (oFile == null)
            {
                errorMsg = "没有找到文件！";
            }
            if (!string.IsNullOrWhiteSpace(errorMsg))
            {
                result.Message = errorMsg;
            }

            try
            {
                int ep_id = 0;
                if (Session[ZPCommon.SESSION_Portal_Login] != null)
                {
                    PortalUserLoginDTO loginUser = (PortalUserLoginDTO)Session[ZPCommon.SESSION_Portal_Login];
                    ep_id = loginUser.user_id;
                }
                if (ep_id == 0)
                {
                    return Json(result);
                }

                string guid = Guid.NewGuid().ToString();
                string filename = guid + System.IO.Path.GetExtension(oFile.FileName); //guid + 文件后缀
                string fileDir = ZPCommon.Upload_TEMP_DIR;    //临时存放目录
                string filepath = Server.MapPath(fileDir) + @"\" + filename; //物理路径
                if (!System.IO.Directory.Exists(Server.MapPath(fileDir)))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(fileDir));
                }
                oFile.SaveAs(filepath);
                BusinessResult res = BusinessHelper.BusinessLicense(filepath);
                result.Object = res;
                result.SubmitResult = true;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 图片表-转excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UploadAnalysisImg2Excel()
        {
            ResultData<string> result = new ResultData<string>();

            var oFile = Request.Files[0];
            //var type = Request.Form["type"];
            string errorMsg = "";
            if (oFile == null)
            {
                errorMsg = "没有找到文件！";
            }
            if (!string.IsNullOrWhiteSpace(errorMsg))
            {
                result.Message = errorMsg;
            }

            try
            {
                string guid = Guid.NewGuid().ToString();
                string filename = guid + System.IO.Path.GetExtension(oFile.FileName); //guid + 文件后缀
                string fileDir = ZPCommon.Upload_TEMP_DIR;    //临时存放目录
                string filepath = Server.MapPath(fileDir) + @"\" + filename; //物理路径
                if (!System.IO.Directory.Exists(Server.MapPath(fileDir)))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(fileDir));
                }
                oFile.SaveAs(filepath);
                var res = BusinessHelper.ConvertImg2Excel(filepath);
                result.Object = res;
                result.SubmitResult = true;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }
            return Json(result);
        }

        
    }
}